<template>
    <el-container class="homecontainer"> 
        <!-- 头部区域 -->
        <el-header>
            <div class="headerContainer">
                <img src="../assets/heima.png" alt="">
                <span class="span">电商后台管理系统</span>
            </div>
            <el-button type="info" @click="logout" class="logoutBtns">退出</el-button>
        </el-header>
        <!-- 页面主体区 -->
        <el-container>
            <el-aside :width = "isCollapse? '64px':'200px'">
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <!-- 侧边栏菜单区域 -->
                <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eef" :unique-opened="true" 
                :collapse="isCollapse" :collapse-transition = "false" router :default-active="activePath">
                    <el-submenu :index="item.id + ''" v-for="item in menusList" :key="item.id">
                        <template slot="title">
                            <i :class="iconslist[item.id]"></i>
                            <span>{{item.authName}}</span>
                        </template>
                        <el-menu-item :index=" '/'+subItem.path " v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/'+subItem.path )">
                            <template slot="title">
                                <i class="el-icon-menu"></i>
                                <span>{{subItem.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
    data() {
        return {
            menusList:[],
            iconslist : {
                '125':'el-icon-setting',
                '103':'el-icon-goods',
                '101':'el-icon-info',
                '102':'el-icon-s-cooperation',
                '145':'el-icon-s-release'
            },
            isCollapse:false,
            activePath:''
        }
    },
    created() {
        this.getMenuList()
        this.activePath = window.sessionStorage.getItem('activepath')
    },
    methods:{
        // 保存连接的激活状态
        saveNavState(activepath) {
            window.sessionStorage.setItem('activepath',activepath)
            this.activePath = activepath
        },
        logout() {
            window.sessionStorage.clear()
            this.$router.push('/login')
        },
        // 获取所有的菜单
        async getMenuList() {
            const {data:res} =  await this.$http.get('menus')
            if(res.meta.status !== 200) return this.$message.error(res.meta.msg);
            this.menusList = res.data
        },
        toggleCollapse() {
            this.isCollapse = !this.isCollapse
        }
    }

}
</script>
<style >
     .headerContainer{
        display: flex;
        align-items: center;
    }
    .span {
        margin-left: 15px;
    }
    .el-header {
    background-color: #373d41;
    color: #333;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-content: center;
    color: #fff;
    font-size: 20px;
  }
  
  .el-aside {
    background-color: #333744;
  }
  .el-menu {
      border-right: none !important;
  }
  
  .el-main {
    background-color: #E9EEF3;
  }
  .homecontainer{
      height: 100%;
  }
  .toggle-button{
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;

  }
</style>
